<?php require_once 'httpFilter.php'; 'conf.php';?>
<?php
require_once 'dto/AutoReconcileDTO.php';

$uploadFileName = $_FILES['transactionFile']['tmp_name'];
$file = fopen($_FILES['transactionFile']['tmp_name'], "r") or exit("Unable to open file!");

$errCode = 0;
$duplicateCheckMap = array();

$accountId = 1;
$accountId = $_REQUEST["accId"];



$con = mysql_connect(GlobalConfig::db_host_name, GlobalConfig::db_username, GlobalConfig::db_password);
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

mysql_select_db(GlobalConfig::db_name, $con);

$query = "select *  from autoreconcile where userid=".$_SESSION["userid"];
error_log($query);
$result1 = mysql_query($query);
if (!$result1) {
	$errCode = 1;
}

//$autoreconciles = array();
$autoReconciles = array();
while($row = mysql_fetch_array($result1)) {
	$autoreconcile = new AutoReconcileDTO($row["budgetitemid"], $row["akey"], $row["avalue"]);
	array_push($autoReconciles, $autoreconcile);
}

$dateFound = false;
$hashkeyFound = false;
while(!feof($file)){
	$aline = fgets($file);

	$line =  split(",", $aline);

	if ($line[0]==""){
		error_log("continuing...");
		continue;
	}

	error_log($aline);
	error_log("ERROR: ".$errCode);
	
	if (strlen(strstr($aline,"Balance:"))>0) {
		$statementBal = $line[1];
		//We update the statement balance
		$query = "update account set stmtBalance=". $statementBal ." where id= ". $accountId;
		error_log($query);
		$result = mysql_query($query) or die(mysql_error());
		if (!$result) {
			$errCode = 1;
		}

		$availableBal = $line[2];
		//We update the available balance
		$query = "update account set available=". $availableBal ." where id= ". $accountId;
		error_log($query);
		$result = mysql_query($query) or die(mysql_error());
		if (!$result) {
			$errCode = 1;
		}
	}

	if ($dateFound){
		$hashkey = md5($line[0].$line[1].$line[2].$line[3]);
		//echo $hashkey;
			
		$dateArray = split("/", $line[0]);
		$date = $dateArray[0]. "-".$dateArray[1]."-".$dateArray[2];
			
		$query = sprintf("insert into transaction (accountId, date, amount, description, reconciled, categoryId, bankRef, hashkey) values (%d, '%s', %f, '', 0, 0, '%s', '%s')",
		$accountId,
		mysql_real_escape_string($date),
		$line[1],
		mysql_real_escape_string($line[3]),
		mysql_real_escape_string($hashkey)
		);

		
		foreach ($autoReconciles as $autoreconcile){
			error_log("RECONCILE: ".$line[3] ." and ". $autoreconcile->budgetitemid);
			if (strlen(stristr($line[3], $autoreconcile->budgetitemid))>0) {
				error_log("FOUND!!! ---->". $autoreconcile->budgetitemid);
				$query = "insert into transaction (accountId, date, amount, description, reconciled, categoryId, bankRef, budgetitemid, hashkey) values (". $accountId .", '". $date ."', ". $line[1] .", '".  $autoreconcile->value ."', 1, (select categoryId from budgetitem where id=". $autoreconcile->key ."), '". $line[3] ."',". $autoreconcile->key .", '". $hashkey ."')";
				break;
			}
		}

		if ($line[3] != ""){
			error_log($query);
			echo $query;
			$result = mysql_query($query);
		}
			

	}
	if (strlen(strstr($aline,"Date"))>0) {
		$dateFound = true;
	}


}

fclose($file);
mysql_close($con);
?>
<result>
<code> <?=$errCode?> </code>
</result>
